package com.apware.utils.service;


import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import com.apware.utils.config.Config;
import com.apware.utils.dao.CarPartsInfoDao;
import com.apware.utils.entity.CarPartsInfo;
import com.apware.utils.util.DateUtil;
import com.apware.utils.util.HttpUtil;
import com.apware.utils.util.SecurityUtil;
import com.apware.utils.util.StringUtil;


public class CarPartService {

	static Logger log = Logger.getLogger(CarPartService.class);
	JRCarPartsInfoReader readerJR = null;
	PYCarPartsInfoReader readerPY= null;
	WDCarPartsInfoReader readerWD = null;
	SDCarPartsInfoReader readerSD = null;
	CarPartsInfoDao carpartDao = new CarPartsInfoDao();

	public void run() {


		while (true) {
			try {

				List<CarPartsInfo> carpartList = null;
				if(Config.erp == null || Config.JINGRIU.equals(Config.erp)){
					readerJR=new JRCarPartsInfoReader();
					carpartList = readerJR.getCarPartsListByJxl();
				}
				else if(Config.erp != null && Config.PENGYU.equals(Config.erp)){
					readerPY=new PYCarPartsInfoReader();
					carpartList = readerPY.getCarPartsListByCVX();
				}
				else if(Config.erp != null && Config.weidechuangjian.equals(Config.erp)){
					readerWD=new WDCarPartsInfoReader();
					carpartList = readerWD.getCarPartsListByCVX();
				}
				else if(Config.erp != null && Config.shide.equals(Config.erp)){
					readerSD=new SDCarPartsInfoReader();
					carpartList = readerSD.getCarPartsListByCSV();
				}
				else {
					carpartList = carpartDao.getCarPartsList();
				}
				if (carpartList !=null && carpartList.size() >0){

					 List<List<CarPartsInfo>>  CarPartsInfoLists =StringUtil.groupListByQuantity(carpartList, 200);//200条一组
					 
					log.info("开始同步商品[数量："+ carpartList.size() +"]...");
					int successCount = 0;
					
					for (List<CarPartsInfo> carPartsInfos : CarPartsInfoLists) {	
						JSONArray jsonArray = new JSONArray();//数组 
						int size=0;
						for (int i = 0; i < carPartsInfos.size() ; i++) {
							CarPartsInfo carPartsInfo=carpartList.get(i);


							if(null == carPartsInfo.getPartsNo() || "".equals(carPartsInfo.getPartsNo())){
								log.warn("商品编码为空！");
								continue;
							}
							if(null == carPartsInfo.getCname() || "".equals(carPartsInfo.getCname())){
								log.warn(carPartsInfo.getPartsNo()+"商品中文名称为空！");
								continue;
							}
							if(null == carPartsInfo.getStandardPartsNo() || "".equals(carPartsInfo.getStandardPartsNo())){
								log.warn(carPartsInfo.getPartsNo()+"品牌编码为空！");
								continue;
							}
							if(null == carPartsInfo.getIsOem() || "".equals(carPartsInfo.getIsOem())){
								log.warn(carPartsInfo.getPartsNo()+"零件品质类型为空！");
								continue;
							}
							if(null == carPartsInfo.getManufactureCname() || "".equals(carPartsInfo.getManufactureCname())){
								log.warn(carPartsInfo.getPartsNo()+"品牌中文名称为空！");
								continue;
							}

							JSONObject json = new JSONObject();
							json.put("supplierCode", Config.supplierCode);
							json.put("cname", carPartsInfo.getCname());
							json.put("partsNo", carPartsInfo.getPartsNo());
							json.put("carInfo", carPartsInfo.getCarInfo());
							json.put("producingArea", carPartsInfo.getProducingArea());
							json.put("price", carPartsInfo.getPrice());
							json.put("stock", carPartsInfo.getStock());
							json.put("standardPartsNo", carPartsInfo.getStandardPartsNo());
							json.put("isOem", carPartsInfo.getIsOem());
							json.put("manufacturerCname", carPartsInfo.getManufactureCname());
							json.put("manufacturerEname", carPartsInfo.getManufactureEname());
							json.put("ename", carPartsInfo.getEname());
							json.put("batchPrice", carPartsInfo.getBatchPrice());
							json.put("unit", carPartsInfo.getUnit());
							json.put("costPrice", carPartsInfo.getCostPrice());
							
							jsonArray.put(json); 
							size++;					
						}
						
						//supplierCode 所属供应商标记 +  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHH") 比如:2017031517
						String sign = Config.supplierCode +DateUtil.format(new Date(), "yyyyMMddHH");          
						sign = SecurityUtil.MD532(sign);//MD5 大写

						HashMap<String, String> para = new HashMap<String, String>();
						para.put("params", jsonArray.toString());
						para.put("sign", sign);
						para.put("supplierCode", Config.supplierCode);
						String res = HttpUtil.post(Config.requestUrl, para );


						/**成功则更新状态为成功发送**/
						JSONObject resjsonObj = null;
						try {
							resjsonObj = new JSONObject(res);
						} catch (Exception ex) {
							log.error("json转化错误！" + res);
						}

						if (resjsonObj != null &&"0000".equals(resjsonObj.getString("code"))){


							successCount+=size;
						}else{
							log.warn("接口报错！返回："+res);
						}
						log.info("成功同步商品数"+ successCount +"");	
					}

				}else{
					log.info("暂无商品需同步0000！");
				}

				Thread.sleep(Config.timerInterval*1000);
			} catch (Exception e) {
				log.error("同步异常！"+e);
			}
		}
	}
}
